MEGpipeline_TimelockFreqAvg.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Group averages timelock or frequency analysis files. %
  3. % Last modified: Feb. 7, 2014 %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. % Copyright (C) 2013-2014, Michael J. Cheung
  6. %
  7. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  8. % details, see the documentation included with the software package.
  9. %
  10. % MEGPLS is free software: you can redistribute it and/or modify it under
  11. % the terms of the GNU General Public License version 2 as published by
  12. % the Free Software Foundation. This program is distributed in the hope
  13. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  14. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. % See the GNU General Public License for more details.
  16. %
  17. % You should have received a copy of the GNU General Public License along
  18. % with this program. If not, you can download the license here:
  19. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  20. function MEGpipeline_TimelockFreqAvg(BuilderMat, AnalysisType)
  21. % Make sure java paths added:
  22. % Note: Using parfor progress bar even though no parfor.
  23. % - Other components already using it, and check needs to be run anyway.
  24. UseProgBar = CheckParforJavaPaths;
  25. % Load builder .mat file:
  26. Builder = load(BuilderMat);
  27. name = Builder.name;
  28. paths = Builder.paths;
  29. FTcfg = Builder.FTcfg;
  30. % Clear existing Errorlog & Diary:
  31. switch AnalysisType
  32. case 'Timelock'
  33. RunSection = 'TimelockAvg';
  34. case 'Freq'
  35. RunSection = 'FreqAvg';
  36. end
  37. if exist(['ErrorLog_',RunSection,'.txt'], 'file')
  38. system(['rm ErrorLog_',RunSection,'.txt']);
  39. end
  40. if exist(['Diary_',RunSection,'.txt'], 'file')
  41. system(['rm Diary_',RunSection,'.txt']);
  42. end
  43. diary(['Diary_',RunSection,'.txt']);
  44. ErrLog = fopen(['ErrorLog_',RunSection,'.txt'], 'a');
  45. %=======================================%
  46. % GROUP-AVERAGE TIMELOCK OR FREQ FILES: %
  47. %=======================================%
  48. for g = 1:length(name.GroupID)
  49. NumCond = length(name.CondID);
  50. if UseProgBar == 1
  51. ppm = ParforProgMon...
  52. (['GROUP-AVERAGING TIME/FREQ DATA: ',name.GroupID{g},'. '], NumCond, 1, 700, 80);
  53. end
  54. % Note: Do NOT parfor here, want proper ordering.
  55. for c = 1:length(name.CondID)
  56. TargetFiles = []; % Reset input
  57. MissingFiles = 0;
  58. % Compile & check subject files to avg:
  59. for s = 1:length(name.SubjID{g})
  60. switch AnalysisType
  61. case 'Timelock'
  62. TargetFiles{s} = paths.Timelock{g}{s,c};
  63. case 'Freq'
  64. TargetFiles{s} = paths.Freq{g}{s,c};
  65. end
  66. CheckInput = CheckPipelineMat(TargetFiles{s}, RunSection);
  67. if CheckInput == 0
  68. MissingFiles = 1;
  69. continue;
  70. end
  71. end % Subj
  72. % Skip condition if errors found:
  73. if MissingFiles == 1
  74. if strcmp(AnalysisType, 'Timelock')
  75. fprintf(ErrLog, ['ERROR: Failed to group-avg data (missing subject data):'...
  76. '\n %s \n\n'], paths.TimelockGrpAvg{g}{c});
  77. elseif strcmp(AnalysisType, 'Freq')
  78. fprintf(ErrLog, ['ERROR: Failed to group-avg data (missing subject data):'...
  79. '\n %s \n\n'], paths.FreqGrpAvg{g}{c});
  80. end
  81. disp('ERROR: Missing .mat file(s) for GPAVG. Skipping this condition.')
  82. continue;
  83. end
  84. % Average files:
  85. switch AnalysisType
  86. case 'Timelock'
  87. CheckSavePerms(paths.TimelockGrpAvg{g}{c}, RunSection);
  88. cfgTimelockAvg = [];
  89. cfgTimelockAvg = FTcfg.TimelockAvg;
  90. cfgTimelockAvg.inputfile = TargetFiles;
  91. cfgTimelockAvg.outputfile = paths.TimelockGrpAvg{g}{c};
  92. cfgTimelockAvg.outputfilepresent = 'overwrite';
  93. ft_timelockgrandaverage(cfgTimelockAvg)
  94. % Compute global mean field power for grpavg timelock data:
  95. if exist(paths.TimelockGrpAvg{g}{c}, 'file')
  96. CheckSavePerms(paths.GlobalMeanFieldGrpAvg{g}{c}, RunSection);
  97. cfgGFP = [];
  98. cfgGFP = FTcfg.GFP;
  99. cfgGFP.inputfile = paths.TimelockGrpAvg{g}{c};
  100. cfgGFP.outputfile = paths.GlobalMeanFieldGrpAvg{g}{c};
  101. cfgGFP.outputfilepresent = 'overwrite';
  102. ft_globalmeanfield(cfgGFP) % Compute GMFP from new timelockgrpavg file.
  103. end
  104. case 'Freq'
  105. CheckSavePerms(paths.FreqGrpAvg{g}{c}, RunSection);
  106. cfgFreqAvg = [];
  107. cfgFreqAvg = FTcfg.FreqAvg;
  108. cfgFreqAvg.inputfile = TargetFiles;
  109. cfgFreqAvg.outputfile = paths.FreqGrpAvg{g}{c};
  110. cfgFreqAvg.outputfilepresent = 'overwrite';
  111. ft_freqgrandaverage(cfgFreqAvg)
  112. end
  113. if UseProgBar == 1 && mod(c, 1) == 0
  114. ppm.increment(); % move up progress bar
  115. end
  116. end % Cond
  117. if UseProgBar == 1
  118. ppm.delete();
  119. end
  120. end % Group
  121. %=========================%
  122. % CHECK FOR OUTPUT FILES: %
  123. %=========================%
  124. for g = 1:length(name.GroupID)
  125. for c = 1:length(name.CondID)
  126. switch AnalysisType
  127. case 'Timelock'
  128. if ~exist(paths.TimelockGrpAvg{g}{c}, 'file')
  129. fprintf(ErrLog, ['ERROR: Output timelock group-avg file missing:'...
  130. '\n %s \n\n'], paths.TimelockGrpAvg{g}{c});
  131. end
  132. if ~exist(paths.GlobalMeanFieldGrpAvg{g}{c}, 'file')
  133. fprintf(ErrLog, ['ERROR: Output global mean field group-avg file missing:'...
  134. '\n %s \n\n'], paths.GlobalMeanFieldGrpAvg{g}{c});
  135. end
  136. case 'Freq'
  137. if ~exist(paths.FreqGrpAvg{g}{c}, 'file')
  138. fprintf(ErrLog, ['ERROR: Output timefreq group-avg file missing:'...
  139. '\n %s \n\n'], paths.FreqGrpAvg{g}{c});
  140. end
  141. end
  142. end % Cond
  143. end % Group
  144. %=================%
  145. if exist([pwd,'/ErrorLog_',RunSection,'.txt'], 'file')
  146. LogCheck = dir(['ErrorLog_',RunSection,'.txt']);
  147. if LogCheck.bytes ~= 0 % File not empty
  148. open(['ErrorLog_',RunSection,'.txt']);
  149. else
  150. delete(['ErrorLog_',RunSection,'.txt']);
  151. end
  152. end
  153. fclose(ErrLog);
  154. diary off